package com.ohs.controller.before;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ohs.pojo.Address;
import com.ohs.pojo.Order_Product;
import com.ohs.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/toMygxin")
    public String toMygxin(Model model,HttpSession session){
        int productDaiShou= this.orderService.selectCountDaiShou(session);
        int productYiShou=this.orderService.selectCountDaiYiShou(session);
        int productUnpaid= this.orderService.selectCountUnpaid(session);
        model.addAttribute("productDaiShou",productDaiShou);
        model.addAttribute("productYiShou",productYiShou);
        model.addAttribute("productUnpaid",productUnpaid);
        return "before/mygxin";
    }
    @RequestMapping("/toMyorderAll")
    public String toMyorderAll(Model model,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,HttpSession session){
        PageHelper.startPage(currentPage,1);
        List<Order_Product> productList1= this.orderService.selectListAll(session);
        PageInfo<Order_Product> pageInfo =new PageInfo<>(productList1);
        String userName=(String)session.getAttribute("userName");
        /*pageInfo.setPageNum(1);*/
        model.addAttribute("userName",userName);
        model.addAttribute("productListAll",pageInfo);
        return "before/myorderAll";
    }
    @RequestMapping("/toMyorderDaiZhiFu")
    public String toMyorderDaiZhiFu(Model model,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,HttpSession session){
        PageHelper.startPage(currentPage,1);
        List<Order_Product> productList1= this.orderService.selectListUnpaid(session);
        String userName=(String)session.getAttribute("userName");
        PageInfo<Order_Product> pageInfo =new PageInfo<>(productList1);
        /*pageInfo.setPageNum(1);*/
        model.addAttribute("userName",userName);
        model.addAttribute("productListUnpaid",pageInfo);
        return "before/myorderDaiZhiFu";
    }
    @RequestMapping("/toMyorderDaiShouhuo")
    public String toMyorderDaiShou(Model model,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,HttpSession session){
        PageHelper.startPage(currentPage,1);
        List<Order_Product> productList = this.orderService.selectListDaishou(session);
        String userName=(String)session.getAttribute("userName");
        PageInfo<Order_Product> pageInfo = new PageInfo<>(productList);
        /*pageInfo.setPageNum(1);*/
        model.addAttribute("userName",userName);
        model.addAttribute("productListDaishou",pageInfo);
        return "before/myorderDaiShouHuo";
    }
    @RequestMapping("/toMyorderYiShou")
    public String toMyorderYiShou(Model model,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,HttpSession session){
        PageHelper.startPage(currentPage,1);
        List<Order_Product> productList2= this.orderService.selectListYiShou(session);
        String userName=(String)session.getAttribute("userName");
        PageInfo<Order_Product> pageInfo = new PageInfo<>(productList2);
        /*pageInfo.setPageNum(1);*/
        model.addAttribute("userName",userName);
        model.addAttribute("productListYiShou",pageInfo);
        return "before/myorderYiShouHuo";
    }
    @RequestMapping("/findOrderByOrderId")
    public String findOrderByOrderId(Integer orderId,Model model,HttpSession session){
        Order_Product order_product = this.orderService.findOrderByOrderId(orderId,session);
        Integer addId=order_product.getAddId();
        Address address = this.orderService.findAddressOne(addId,session);
        model.addAttribute("orderPr",order_product);
        model.addAttribute("address",address);
        return "before/orderxq";
    }
    @RequestMapping("/findOrder")
    public String findOrder_Product(String proName,Model model,HttpSession session){
        List<Order_Product> list = this.orderService.findOrder_Product(proName,session);
        String userName=(String)session.getAttribute("userName");
        /*pageInfo.setPageNum(1);*/
        model.addAttribute("userName",userName);
        model.addAttribute("orderPro",list);
        return "before/myorderCheck";
    }
    @RequestMapping("/toMyprod")
    public String toMyprod(){
        return "before/myprod";
    }

    @RequestMapping("/updateOrderByOrderId")
    public String doUpdateOrderByOrderId(Integer orderId,HttpSession session){
        return this.orderService.updateOrderByOrderId(orderId);
    }
}
